<template>
    <div class="common-layout">
        <el-container>
            <el-header class="header-wrapped">
                <div class="header-content">
                    <h3>通用后台管理系统</h3>
                    <span class="welcome">欢迎您的登录</span>
                </div>
            </el-header>
            <el-main>
                <div class="login-wapped">
                    <el-card class="box-card">
                        <el-tabs v-model="activeName" class="demo-tabs" :stretch="true">
                            <el-tab-pane label="登录" name="login">
                                <el-form :model="loginData" class="login-form">
                                    <el-form-item label="账号">
                                        <el-input v-model="loginData.account" placeholder="请输入账号" />
                                    </el-form-item>
                                    <el-form-item label="密码">
                                        <el-input v-model="loginData.password" placeholder="请输入密码" />
                                    </el-form-item>
                                    <div class="footer-wrapped">
                                        <div class="forget-password">
                                            <span class="forget-password-button" @click="openForget">忘记密码</span>
                                        </div>
                                        <div class="footer-button">
                                            <el-button type="primary" @click="handleLogin">登录</el-button>
                                        </div>
                                        <div class="footer-go-register">
                                            还没有账号？<span class="go-register">马上注册</span>
                                        </div>
                                    </div>

                                </el-form>

                            </el-tab-pane>
                            <el-tab-pane label="注册" name="register">
                                <el-form :model="registerData" label-width="auto" class="login-form">
                                    <el-form-item label="账号">
                                        <el-input v-model="registerData.account" placeholder="账号长度6-12位" />
                                    </el-form-item>
                                    <el-form-item label="密码">
                                        <el-input v-model="registerData.password" placeholder="密码需长度6-12位含字母数字" />
                                    </el-form-item>
                                    <el-form-item label="确认密码">
                                        <el-input v-model="registerData.repassword" placeholder="请再次输入密码" />
                                    </el-form-item>
                                    <div class="footer-wrapped">
                                        <div class="footer-button">
                                            <el-button type="primary" @click="handleRegister">注册</el-button>
                                        </div>
                                    </div>
                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </el-card>

                </div>
            </el-main>
            <el-footer class="footer-wapped">
                <div class="footer-content">
                    <div class="title">
                        <span>阿里云博客专家</span> |
                        <span>阿里云博客专家</span> |
                        <span>阿里云博客专家</span>
                    </div>

                </div>
            </el-footer>
        </el-container>
    </div>
    <forgetPassword ref="forgetP"></forgetPassword>
</template>

<script setup lang='ts'>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router';
import forgetPassword from './components/forget_password.vue';
import { login, register } from '@/api/login';

const activeName = ref('login')
const router = useRouter();

interface formData {
    account: number | null;
    password: string;
    repassword?: string;
}

const loginData: formData = reactive({
    account: null,
    password: ''
})

const registerData: formData = reactive({
    account: null,
    password: '',
    repassword: ''
});

//登录
const handleLogin = async () => {
    const res = await login(loginData);
    if (res.data.message === '登陆成功') {
        const { token } = res?.data;
        localStorage.setItem('token', token)
        ElMessage({
            message: res.data.message,
            type: 'success',
        });
        router.push('/home')
    } else {
        ElMessage.error('登录失败，' + res.data.message)
    }
}

//注册
const handleRegister = async () => {
    if (registerData.password !== registerData.repassword) {
        return ElMessage.error('注册失败，请检查密码是否一致')
    }
    const res = await register(registerData);
    if (res.data.message === '注册账号成功') {
        ElMessage({
            message: res.data.message,
            type: 'success',
        });
        activeName.value = 'login'
    } else {
        ElMessage.error('注册失败，' + res.data.message)
    }
}



const forgetP = ref();

//忘记密码弹窗
const openForget = () => {
    forgetP.value.open()
}
</script>

<style lang="scss" scoped>
.header-wrapped {
    .header-content {
        width: 1200px;
        margin: 0 auto;
        display: flex;
        justify-content: space-between;
        align-items: center;

        .welcome {
            font-size: 13x;
        }
    }
}

.el-main {
    height: 600px;
    --el-main-padding: 0;
    background-color: rgb(67, 67, 242);

    .login-wapped {

        height: 600px;
        width: 1200px;
        margin: 0 auto;

        .box-card {
            width: 350px;
            height: 375px;
            float: right;
            position: relative;
            top: 14%;

            .login-form {
                .footer-wrapped {
                    display: flex;
                    flex-direction: column;

                    .forget-password {
                        display: flex;
                        justify-content: flex-end;
                        margin-top: 10px;

                        .forget-password-button {
                            color: #409eff;
                            font-size: 12px;
                            cursor: pointer;
                        }
                    }
                }

                .footer-button {
                    width: 100%;
                    display: flex;
                    justify-content: center;
                }

                .footer-go-register {
                    font-size: 12px;
                    margin: 12px 0;
                    display: flex;
                    justify-content: center;

                    .go-register {
                        color: #409eff;
                        font-size: 12px;
                        cursor: pointer;
                    }
                }
            }
        }
    }
}



.footer-wapped {
    margin-top: 8px;

    .footer-content {
        width: 1200px;
        margin: 0 auto;
        display: flex;
        justify-content: center;

        .title {
            color: #666;
            display: flex;
            align-items: center;
            font-size: 12px;
        }

        span {
            color: #666;
            font-size: 12px;
        }
    }
}

.el-form {
    margin-top: 30px;
}

:deep(.el-tabs__item) {
    color: #333;
    font-size: 18px;
}

:deep(.el-input__inner) {
    height: 40px;
}

:deep(.el-form-item__label) {
    height: 40px;
    line-height: 40px;
}

:deep(.el-button) {
    width: 300px;
    height: 45px;
    font-size: 16px;
}
</style>